from django.shortcuts import render, HttpResponse, redirect
from web.forms.account import RegisterModelForm, SendSmsForm, LoginForm
from django.http import JsonResponse
from utils.imagecode import check_code
from io import BytesIO
from django.views.decorators.csrf import csrf_exempt
from web import models
from django.db.models import Q
import cv2
import json
import paho.mqtt.client as mqtt

import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}


def get_html(url):
    response = requests.get(url, headers=headers)
    if response.status_code != 200:
        raise Exception("error")
    return response.text


def parse_new_html(html):
    soup = BeautifulSoup(html, 'html.parser')
    lis = soup.find_all("li", class_="clearfix")
    news = []
    for li in lis:
        img_node = (
            li.find('a').find('img')
        )
        title_node = (
            li.find('div', class_="HotConLText").find("h4").find("a")
        )
        detail_node = (
            li.find('div', class_="HotConLText").find("p")
        )
        title = title_node.get_text()
        link = title_node["href"]
        details = detail_node.get_text()
        if len(str(details)) > 100:
            detail = '{}...'.format(str(details)[0:100])

        else:

            detail = str(details)

        img = img_node["src"]
        news.append({"title": title, "link": link, "detail": detail, "img": img})
    return news


def parse_exam_html(html):
    soup = BeautifulSoup(html, 'html.parser')
    divs = soup.find_all("li", class_="clearfix")
    exams = []
    for div in divs:
        img_node = (
            div.find('a').find('img')
        )
        title_node = (
            div.find('div', class_="HotConLText").find("h4").find("a")
        )
        detail_node = (
            div.find('div', class_="HotConLText").find("p")
        )
        title = title_node.get_text()
        link = title_node["href"]
        details = detail_node.get_text()
        if len(str(details)) > 80:
            detail = '{}...'.format(str(details)[0:80])
        else:
            detail = str(details)
        img = img_node["src"]
        exams.append({"title": title, "link": link, "detail": detail, "img": img})
    return exams


def parse_prog_html(html):
    soup = BeautifulSoup(html, 'html.parser')
    divs = soup.find_all("li", class_="clearfix")
    progs = []
    for div in divs:
        img_node = (
            div.find('a').find('img')
        )
        title_node = (
            div.find('div', class_="HotConLText").find("h4").find("a")
        )
        detail_node = (
            div.find('div', class_="HotConLText").find("p")
        )
        title = title_node.get_text()
        link = title_node["href"]
        details = detail_node.get_text()
        if len(str(details)) > 80:
            detail = '{}...'.format(str(details)[0:80])
        else:
            detail = str(details)
        img = img_node["src"]
        progs.append({"title": title, "link": link, "detail": detail, "img": img})
    return progs


def parse_prod_html(html):
    soup = BeautifulSoup(html, 'html.parser')
    soup.prettify()
    divs = soup.find_all("li", class_="clearfix")
    prods = []
    for div in divs:
        img_node = (
            div.find('a').find('img')
        )
        title_node = (
            div.find('div', class_="HotCon03_con").find("h5").find("a")
        )
        detail_node = (
            div.find('div', class_="HotCon03_con").find("p")
        )
        field_node = (
            div.find('div', class_="HotCon03_con").find('div', class_="clearfix").find_all('b')
        )
        field1 = field_node[0].get_text()
        field2 = field_node[1].get_text()
        title = title_node.get_text()
        link = title_node["href"]
        details = detail_node.get_text()
        if len(str(details)) > 80:
            detail = '{}...'.format(str(details)[0:80])
        else:
            detail = str(details)
        img = img_node["src"]
        prods.append({"title": title, "link": link, "detail": detail, "img": img, "field1": field1, "field2": field2})

    return prods


def test(request):
    new_url = 'http://www.iotworld.com.cn/News.html'
    exam_url = 'http://www.iotworld.com.cn/SuccList.html?order=hot'
    prog_url = 'http://www.iotworld.com.cn/SolList.html?appId=205'
    prod_url = 'http://www.iotworld.com.cn/Products.html'

    exam_html = get_html(exam_url)
    exams = parse_exam_html(exam_html)

    new_html = get_html(new_url)
    news = parse_new_html(new_html)

    prog_html = get_html(prog_url)
    progs = parse_prog_html(prog_html)

    prod_html = get_html(prod_url)
    prods = parse_prod_html(prod_html)
    return render(request, 'search.html', {'progs': progs, 'news': news, 'exams': exams, 'prods': prods})
